package org.lanqiao.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.lanqiao.pojo.CartListPOJO;
import org.lanqiao.pojo.ProductPOJO;
import org.lanqiao.util.SqlUtil;
/**
 * 购物车dao层
 * @author dzp
 *
 */
public class CartDaoImpl {
		
	/**
	 * 1.加入购物车，根据传递的商品信息，加入购物车,同一个商品加入购物车只增加商品数量
	 */
	public int  addCart(String userId,String productId,String productCount,String totalPrice) {
		int row = 0;
		//传入用户Id,购物车状态,
		String sqls = "SELECT * FROM car_list c WHERE c.CAR_PRODUCT_ID = ? AND c.CAR_USER_ID = ? AND c.CAR_STATUS = 1 ";
		List<LinkedHashMap<String,Object>> list = SqlUtil.select3(sqls,productId,userId);	
		//获得价格
		String sql41 = "SELECT PRODUCT_PRICE FROM  product  WHERE PRODUCT_ID = ? ";
		List<LinkedHashMap<String,Object>> list2 =	SqlUtil.select3(sql41,productId);
		if(list.size()>0 && list!=null) {
			//获得商品总数
			int a=Integer.valueOf(productCount);
			int b = Integer.valueOf(list.get(0).get("carProductCount").toString()) ;
			int count=a+b;
			System.out.println(count);
			//获取商品单价
			double price = Double.valueOf(list2.get(0).get("productPrice").toString());
			System.out.println(price);
			double totalPrice1 = count*price;
			System.out.println(totalPrice1);
			String sqls2 = "UPDATE car_list c SET CAR_PRODUCT_COUNT =? , CAR_TOTAL_PRICE = ? WHERE c.CAR_PRODUCT_ID = ?  AND c.CAR_USER_ID =? AND c.CAR_STATUS = 1" ;                            
			row = SqlUtil.update(sqls2,count,totalPrice1,productId,userId);
			return 1;
		}else {
			String sql = "insert into CAR_LIST (CAR_ID,CAR_USER_ID,CAR_PRODUCT_ID,CAR_PRODUCT_COUNT,CAR_TOTAL_PRICE,CAR_STATUS,CAR_DATE_TIME) VALUES ('1',?,?,?,?,1,NOW())";                                       
			row= SqlUtil.update(sql,userId,productId,productCount,totalPrice);      
		}
		return row;
	}
	
	/**
	 * 2.查询购物车，通过两表联查，用户ID,按照购物车加入时间降序排序
	 */
	
	public List SelectAllCart(String userId) {
		String sql2 ="SELECT cl.*,pt.* FROM car_list cl LEFT JOIN product pt ON cl.CAR_PRODUCT_ID  = pt.PRODUCT_ID WHERE cl.CAR_USER_ID = ? AND cl.CAR_ORDER_ID is NULL and cl.CAR_STATUS=1  ORDER BY cl.CAR_DATE_TIME desc ";
		return SqlUtil.select3(sql2, userId);
		
	}

	
	/**
	 * 3.移除购物车，把购物车状改为0，随后立即进行更新
	 */
	
	public int deleteCart(String carId) {
		 
		String sql3 = "UPDATE car_list SET CAR_STATUS = 0 WHERE CAR_ID =? ";
		return SqlUtil.update(sql3, carId);
				
	}
	
	/**
	 * 4.修改购物车信息，只能修改商品数量数量参数,但是商品总价也会改变
	 */
	
	public int updateCart(CartListPOJO cartlist ) {
		//两表联查获取商品价格
//		String sql41 = "SELECT p.PRODUCT_PRICE FROM car_list c LEFT JOIN product p on  c.CAR_PRODUCT_ID=P.PRODUCT_ID WHERE c.CAR_ID = ? ";
//		List<LinkedHashMap<String,Object>> list =	SqlUtil.select3(sql41, cartlist.getId());
//		System.out.println(list.get(0).get("productPrice").toString());
//		System.out.println(cartlist.getProductCount());
//		Double b = Double.valueOf(list.get(0).get("productPrice").toString());
//		int a = Integer.parseInt(cartlist.getProductCount());
//		double totalPrice = a*b;
//		System.out.println(totalPrice);
		String sql4 = "UPDATE car_list SET CAR_PRODUCT_COUNT = ?, CAR_TOTAL_PRICE = ? WHERE CAR_ID = ?  " ;                            
		return SqlUtil.update(sql4, cartlist.getProductCount(),cartlist.getTotalPrice(),cartlist.getId());
		
	}
}
